package com.killer.mianshibaodian;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.killer.mianshibaodian.utils.QuestionDao;

import java.util.List;
import java.util.Map;

public class QuestionActivity extends BaseActivity {

    //显示数据使用
    private ListView list;
    private List<Map<String, Object>> values;
    private SimpleAdapter adapter;

    //分页使用
    private int pagenum = 1;
    private int pagesize = 15;
    private int recordCount;
    private int pageCount;
    private String keyword;

    //list的onScroll事件使用
    private int first;
    private int visible;
    private int total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        super.init("全部问题", 1);

        list = (ListView) findViewById(R.id.list);

        keyword = getIntent().getStringExtra("keyword");
        if (keyword==null){
            keyword="";
        }
        // values=QuestionDao.loadData();
        //分页查询数据
        values = QuestionDao.loadPage(pagenum, pagesize, keyword);

        //匹配模糊查询总记录数
        recordCount = QuestionDao.getCount(keyword);
        //总页数
        pageCount = (recordCount-1)/pagesize+1;
        //加入尾部footView
        final TextView footerView = new TextView(this);
        footerView.setText("正在查询，请稍后");
        footerView.setTextSize(12);
        footerView.setTextColor(Color.BLACK);
        list.addFooterView(footerView);

       // Log.i("killer",values.size()+"ssssss");
        adapter = new SimpleAdapter(this,values,R.layout.question_list,new String[]{"question"},new int[]{R.id.list_question});
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = values.get(position);
                LinearLayout linearLayout = (LinearLayout) view;
                //只有一行，加入答案
                //否则删除答案
                if (linearLayout.getChildCount() == 1) {
                    TextView ansView = new TextView(QuestionActivity.this);
                    ansView.setText(map.get("answer").toString());
                    ansView.setTextSize(14);
                    ansView.setTextColor(Color.RED);
                    linearLayout.addView(ansView);
                } else {
                    linearLayout.removeViewAt(1);
                }
            }
        });

        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState==SCROLL_STATE_IDLE&&first+visible==total&&total !=0){
                    if (pagenum<pageCount){
                        pagenum++;
                        //将剩下的数据载入到原有数据后
                        values.addAll(QuestionDao.loadPage(pagenum,pagesize,keyword));
                        //通知
                        adapter.notifyDataSetChanged();
                    }else {
                        //如果到底了，去除footView
                        if (list.getFooterViewsCount()>0){
                            list.removeFooterView(footerView);
                        }

                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    first = firstVisibleItem;
                    visible = visibleItemCount;
                    total = totalItemCount;
            }
        });
    }
}
